package com.ruoyi.module.system.controller;

import com.ruoyi.web.annotations.ApiController;
import com.ruoyi.core.entity.BaseMap;
import com.ruoyi.core.entity.KV;
import com.ruoyi.core.entity.Output;
import com.ruoyi.web.entity.system.LoginVo;
import com.ruoyi.web.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@ApiController
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/getAuthCode")
    public Output<BaseMap> getAuthCode() {
        KV authCode = loginService.getAuthCode();
        BaseMap baseMap = new BaseMap();
        baseMap.put("authKey", authCode.getK());
        baseMap.put("authCode", authCode.getV());
        return Output.success(baseMap);
    }

    @PostMapping("/login")
    public Output<BaseMap> login(@RequestBody LoginVo loginVo) {
        String token = loginService.login(loginVo.getUserName(), loginVo.getPassword(), loginVo.getAuthKey(), loginVo.getAuthCode());
        BaseMap baseMap = new BaseMap();
        baseMap.put("accessToken", token);
        return Output.success(baseMap);
    }

    @PostMapping("/logout")
    public Output<Object> logout() {
        loginService.logout();
        return Output.success(null);
    }

}
